/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.control;

import edu.hpvas.persistence.dao.MedicalRecordDAO;
import edu.hpvas.persistence.dao.OwnerDAO;
import edu.hpvas.persistence.dao.PetDAO;
import edu.hpvas.persistence.entity.Appointment;
import edu.hpvas.persistence.entity.MedicalRecord;
import edu.hpvas.persistence.entity.Owner;
import edu.hpvas.persistence.entity.Pet;
import edu.hpvas.persistence.entity.Veterinarian;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.IOUtils;

/**
 *
 * @author sony
 */
public class PetController {
    private Pet pet;
    private PetDAO petDao = new PetDAO();
    private OwnerDAO ownerDao = new OwnerDAO();
    private MedicalRecordDAO medicalDao= new MedicalRecordDAO();
    public PetController(){}
    
    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public PetDAO getPetDao() {
        return petDao;
    }

    public void setPetDao(PetDAO petDao) {
        this.petDao = petDao;
    }
    
    public Pet searchPets()
    {
        return new Pet();
    }
    public ArrayList<String> getSpecies(){
        List<Pet> pets = petDao.findPetEntities();
        ArrayList<String> species= new ArrayList<>();
        if(pets!=null){
            for(Pet p: pets){
                if(species.indexOf(p.getSpecies())!=-1){
                    species.add(p.getSpecies());     
                }
            }
            return species;
        }
        return new ArrayList<>();
        
    }
    public void createPet(Owner owner, Pet pet){
        // has an error
        int idMedical = medicalDao.getMedicalRecordCount()+1;
        try {
            medicalDao.create(new MedicalRecord(new Integer(idMedical)));
        } catch (Exception ex) {
            Logger.getLogger(PetController.class.getName()).log(Level.SEVERE, null, ex);
        }
        pet.setIdMedicalRecord(new MedicalRecord(idMedical));
        //owner.getPetCollection().add(pet);
        pet.setIdOwner(owner);
        try {
            ownerDao.edit(owner);
            petDao.create(pet);
        } catch (Exception ex) {
            Logger.getLogger(PetController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public ArrayList<Pet> petByVeterinarian(Veterinarian idVeterinarian){
        
        ArrayList pets = new ArrayList();
        AppointmentController appointmentController = new AppointmentController();
        ArrayList<Appointment> appoints = appointmentController.getByVet(idVeterinarian);
        int IdMedica;
        List<Pet> petsList =  petDao.findPetEntities();
        for(Appointment a: appoints){
            for(Pet p: petsList){
            if(a.getIdMedicalRecord().getId().intValue() == p.getIdMedicalRecord().getId().intValue()){
                pets.add(p);
                break;
            }
        }
        }
        
        return pets;
    }
    public ArrayList<Pet> petByOwner(Integer Idowner){
        
        ArrayList pets = new ArrayList();
        List<Pet> petsList =  petDao.findPetEntities();
        for(Pet p: petsList){
            if( p.getIdOwner() != null && p.getIdOwner().getId().intValue()==Idowner.intValue()){
                pets.add(p);
            }
        }
        return pets;
    }
   
    public boolean updatePet(Pet pet,Pet newPet){
        try {
            petDao.edit(pet,newPet);
            return true;
        } catch (Exception ex) {
            Logger.getLogger(PetController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    public ArrayList<Pet> getPets(String name)
    {
        List<Pet> pets = petDao.findPetsEntitiesByName(name);
        ArrayList<Pet> petsA = null;
        if(pets!=null){
            petsA = new ArrayList<>(pets);
        }
        return petsA;
    }
    public void deletePet(Pet pet){
        petDao.destroy(pet.getId());
    }
    
     
}
